package com.yss.sofa.report.engine.util;

public final class ArrayUtil {
	/**
	 * 为一个二维数组分配新空间，并将原来数组的内容拷贝到新数组中去
	 * 
	 * @param oldArray
	 *            the old array, to be reallocated.
	 * @param newRowSize
	 *            row size of the new array
	 * @param newColumnSize
	 *            column size of the new array.
	 * @return A new array
	 */
	public static Object resize2DArray(Object oldArray, int newRowSize,
			int newColumnSize) {
		Object[][] old2DArray = (Object[][]) oldArray;
		Object[][] new2DArray = (Object[][]) java.lang.reflect.Array
				.newInstance(old2DArray.getClass().getComponentType(),
						newRowSize);
		String className = new2DArray.getClass().getName();
		className = className.substring(className.indexOf("[L") + 2,
				className.length() - 1);
		Class elementType = null;
		try {
			elementType = Class.forName(className);
		} catch (ClassNotFoundException e) {
			// 打开注释部分兼容性更强 适合复杂情况的非基础数据类型的加载
			// try {
			// elementType = Class.forName(className, false,
			// Thread.currentThread()
			// .getContextClassLoader());
			// } catch (ClassNotFoundException e1) {
			// try {
			// elementType = Class.forName(className, false, ResizeArray.class
			// .getClassLoader());
			// } catch (ClassNotFoundException e2) {
			// e2.printStackTrace();
			// }
			// }
			e.printStackTrace();
		}
		for (int i = 0; i < old2DArray.length; i++) {
			new2DArray[i] = (Object[]) java.lang.reflect.Array.newInstance(
					elementType, newColumnSize);

			System.arraycopy(old2DArray[i], 0, new2DArray[i], 0,
					Math.min(old2DArray[i].length, newColumnSize));

		}
		return new2DArray;
	}
}
